import 'dart:ffi';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
// import 'package:device_info_plus/device_info_plus.dart';
import 'package:device_info/device_info.dart';
import 'package:flutter/services.dart';
import 'package:flutter_rider/utils/extension.dart';
import 'package:package_info_plus/package_info_plus.dart';

class DeviceUtils {
  static final DeviceUtils _instance = DeviceUtils();
  static final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
  late final PackageInfo packageInfo;
  Map<String, dynamic> _deviceData = <String, dynamic>{};
  static DeviceUtils get instance {
    return _instance;
  }

  Future<void> initPlatformState() async {
    var deviceData = <String, dynamic>{};

    try {
      // if (kIsWeb) {
      //   deviceData = _readWebBrowserInfo(await deviceInfoPlugin.webBrowserInfo);
      // } else {
      if (Platform.isAndroid) {
        deviceData = _readAndroidBuildData(await deviceInfoPlugin.androidInfo);
      } else if (Platform.isIOS) {
        deviceData = _readIosDeviceInfo(await deviceInfoPlugin.iosInfo);
      }
      //    else if (Platform.isLinux) {
      //     deviceData = _readLinuxDeviceInfo(await deviceInfoPlugin.linuxInfo);
      //   } else if (Platform.isMacOS) {
      //     deviceData = _readMacOsDeviceInfo(await deviceInfoPlugin.macOsInfo);
      //   } else if (Platform.isWindows) {
      //     deviceData =
      //         _readWindowsDeviceInfo(await deviceInfoPlugin.windowsInfo);
      //   }
      // }
    } on PlatformException {
      deviceData = <String, dynamic>{
        'Error:': 'Failed to get platform version.'
      };
    }

    _deviceData = deviceData;

    packageInfo = await PackageInfo.fromPlatform();
  }

  Map<String, dynamic> getInfo() {
    return _deviceData;
  }

  ///是否是Android 29版本以上 true就是可以使用TTS 其它平台默认使用TTS
  bool getIsTSS() {
    if (Platform.isAndroid) {
      var version = toInt(_deviceData["version.sdkInt"] ?? 0);
      return version >= 29;
    } else {
      return true;
    }
  }

  Map<String, dynamic> _readAndroidBuildData(AndroidDeviceInfo build) {
    return <String, dynamic>{
      'version.securityPatch': build.version.securityPatch,
      'version.sdkInt': build.version.sdkInt,
      'version.release': build.version.release,
      'version.previewSdkInt': build.version.previewSdkInt,
      'version.incremental': build.version.incremental,
      'version.codename': build.version.codename,
      'version.baseOS': build.version.baseOS,
      'board': build.board,
      'bootloader': build.bootloader,
      'brand': build.brand,
      'device': build.device,
      'display': build.display,
      'fingerprint': build.fingerprint,
      'hardware': build.hardware,
      'host': build.host,
      'id': build.id,
      'manufacturer': build.manufacturer,
      'model': build.model,
      'product': build.product,
      'supported32BitAbis': build.supported32BitAbis,
      'supported64BitAbis': build.supported64BitAbis,
      'supportedAbis': build.supportedAbis,
      'tags': build.tags,
      'type': build.type,
      'isPhysicalDevice': build.isPhysicalDevice,
      'androidId': build.androidId,
      'systemFeatures': build.systemFeatures,
    };
  }

  Map<String, dynamic> _readIosDeviceInfo(IosDeviceInfo data) {
    return <String, dynamic>{
      'name': data.name,
      'systemName': data.systemName,
      'systemVersion': data.systemVersion,
      'model': data.model,
      'localizedModel': data.localizedModel,
      'identifierForVendor': data.identifierForVendor,
      'isPhysicalDevice': data.isPhysicalDevice,
      'utsname.sysname': data.utsname.sysname,
      'utsname.nodename': data.utsname.nodename,
      'utsname.release': data.utsname.release,
      'utsname.version': data.utsname.version,
      'utsname.machine': data.utsname.machine,
    };
  }

   // iPhone
    // if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 2G";
    // if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
    // if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
    // if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
    // if ([platform isEqualToString:@"iPhone3,2"]) return @"iPhone 4";
    // if ([platform isEqualToString:@"iPhone3,3"]) return @"iPhone 4";
    // if ([platform isEqualToString:@"iPhone4,1"]) return @"iPhone 4S";
    // if ([platform isEqualToString:@"iPhone5,1"]) return @"iPhone 5";
    // if ([platform isEqualToString:@"iPhone5,2"]) return @"iPhone 5";
    // if ([platform isEqualToString:@"iPhone5,3"]) return @"iPhone 5c";
    // if ([platform isEqualToString:@"iPhone5,4"]) return @"iPhone 5c";
    // if ([platform isEqualToString:@"iPhone6,1"]) return @"iPhone 5s";
    // if ([platform isEqualToString:@"iPhone6,2"]) return @"iPhone 5s";
    // if ([platform isEqualToString:@"iPhone7,1"]) return @"iPhone 6 Plus";
    // if ([platform isEqualToString:@"iPhone7,2"]) return @"iPhone 6";
    // if ([platform isEqualToString:@"iPhone8,1"]) return @"iPhone 6s";
    // if ([platform isEqualToString:@"iPhone8,2"]) return @"iPhone 6s Plus";
    // if ([platform isEqualToString:@"iPhone8,4"]) return @"iPhone SE";
    // if ([platform isEqualToString:@"iPhone9,1"]) return @"iPhone 7";
    // if ([platform isEqualToString:@"iPhone9,2"]) return @"iPhone 7 Plus";
    // if ([platform isEqualToString:@"iPhone10,1"]) return @"iPhone 8";
    // if ([platform isEqualToString:@"iPhone10,4"]) return @"iPhone 8";
    // if ([platform isEqualToString:@"iPhone10,2"]) return @"iPhone 8 Plus";
    // if ([platform isEqualToString:@"iPhone10,5"]) return @"iPhone 8 Plus";
    // if ([platform isEqualToString:@"iPhone10,3"]) return @"iPhone X";
    // if ([platform isEqualToString:@"iPhone10,6"]) return @"iPhone X";
    // if ([platform isEqualToString:@"iPhone11,2"]) return @"iPhone XS";
    // if ([platform isEqualToString:@"iPhone11,6"]) return @"iPhone XS MAX";
    // if ([platform isEqualToString:@"iPhone11,8"]) return @"iPhone XR";
    // if ([platform isEqualToString:@"iPhone12,1"]) return @"iPhone 11";
    // if ([platform isEqualToString:@"iPhone12,3"]) return @"iPhone 11 Pro";
    // if ([platform isEqualToString:@"iPhone12,5"]) return @"iPhone 11 Pro Max";
    // if ([platform isEqualToString:@"iPhone12,8"]) return @"iPhone SE (2nd generation)";
    // if ([platform isEqualToString:@"iPhone13,1"]) return @"iPhone 12 mini";
    // if ([platform isEqualToString:@"iPhone13,2"]) return @"iPhone 12";
    // if ([platform isEqualToString:@"iPhone13,3"]) return @"iPhone 12 Pro";
    // if ([platform isEqualToString:@"iPhone13,4"]) return @"iPhone 12 Pro Max";


  String getSystemInfo() {
    var info = "";
    if (Platform.isAndroid) {
      info = "\nversion.sdkInt:${_deviceData["version.sdkInt"] ?? ""}\nversion.release:${_deviceData["version.release"] ?? ""}\nbrand:${_deviceData["brand"] ?? ""}\nmodel:${_deviceData["model"] ?? ""}";
    } else if (Platform.isIOS) {
      info =
          "\nsystemName:${_deviceData["systemName"] ?? ""}\nsystemVersion:${_deviceData["systemVersion"] ?? ""}\nutsname.machine:${_deviceData["utsname.machine"] ?? ""}";
    }

    return info;
  }

  // Map<String, dynamic> _readLinuxDeviceInfo(LinuxDeviceInfo data) {
  //   return <String, dynamic>{
  //     'name': data.name,
  //     'version': data.version,
  //     'id': data.id,
  //     'idLike': data.idLike,
  //     'versionCodename': data.versionCodename,
  //     'versionId': data.versionId,
  //     'prettyName': data.prettyName,
  //     'buildId': data.buildId,
  //     'variant': data.variant,
  //     'variantId': data.variantId,
  //     'machineId': data.machineId,
  //   };
  // }

  // Map<String, dynamic> _readWebBrowserInfo(WebBrowserInfo data) {
  //   return <String, dynamic>{
  //     'browserName': describeEnum(data.browserName),
  //     'appCodeName': data.appCodeName,
  //     'appName': data.appName,
  //     'appVersion': data.appVersion,
  //     'deviceMemory': data.deviceMemory,
  //     'language': data.language,
  //     'languages': data.languages,
  //     'platform': data.platform,
  //     'product': data.product,
  //     'productSub': data.productSub,
  //     'userAgent': data.userAgent,
  //     'vendor': data.vendor,
  //     'vendorSub': data.vendorSub,
  //     'hardwareConcurrency': data.hardwareConcurrency,
  //     'maxTouchPoints': data.maxTouchPoints,
  //   };
  // }

  // Map<String, dynamic> _readMacOsDeviceInfo(MacOsDeviceInfo data) {
  //   return <String, dynamic>{
  //     'computerName': data.computerName,
  //     'hostName': data.hostName,
  //     'arch': data.arch,
  //     'model': data.model,
  //     'kernelVersion': data.kernelVersion,
  //     'osRelease': data.osRelease,
  //     'activeCPUs': data.activeCPUs,
  //     'memorySize': data.memorySize,
  //     'cpuFrequency': data.cpuFrequency,
  //     'systemGUID': data.systemGUID,
  //   };
  // }

  // Map<String, dynamic> _readWindowsDeviceInfo(WindowsDeviceInfo data) {
  //   return <String, dynamic>{
  //     'numberOfCores': data.numberOfCores,
  //     'computerName': data.computerName,
  //     'systemMemoryInMegabytes': data.systemMemoryInMegabytes,
  //   };
  // }
}
